package com.zoe.app.controller;

import com.zoe.app.contextHolder.ContextHolder;
import com.zoe.app.entity.SysUser;
import com.zoe.app.event.CommonEvent;
import com.zoe.app.event.ZoeEvent;
import com.zoe.app.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Zoe.Zhang
 * @date 2021/10/28
 * @description
 */
@RestController
public class HelloController {

    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private SysUserService sysUserService;

    // http://localhost:8080/hello/123123/
    @GetMapping("/hello/{msg}")
    public String hello(@PathVariable("msg") String msg) {
        ContextHolder.setMsg(msg);
        // 事件发送和事件监听是同一个线程，也就是说如果事件监听发生了阻塞，也会导致这里阻塞
        applicationContext.publishEvent(new ZoeEvent(this, msg));
        System.out.println("controller:" + Thread.currentThread().getName());
        applicationContext.publishEvent(new CommonEvent(this, msg));
        return msg;
    }

    // http://localhost:8080/user
    @GetMapping("/user")
    public String user() {
        SysUser sysUser = new SysUser();
        sysUser.setName("西门");
        boolean save = sysUserService.saveTransaction(sysUser);
        return "ok";
    }

}
